<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_news extends MY_Model { 
	protected $offset = 0;
	protected $limit = 30;
	function __construct() {
		parent::__construct();
	}
	function get_all()
	{
		$this->db->select('p.*')
			->from('news p')
			->order_by('p.id','asc')
			->limit($this->limit, $this->offset);
		return $this->db->get()->result_array();
	}

	function get_next($id){
		$this->db->select('p.*')
			->from('news p')
			->where('p.id >',$id)
			->order_by('p.id','asc')
			->limit(1);
		return $this->db->get()->row_array();
	}

	function get_prev($id){
		$this->db->select('p.*')
			->from('news p')
			->where('p.id <',$id)
			->order_by('p.id','desc')
			->limit(1);
		return $this->db->get()->row_array();
	}

	function get($id='')
	{
		return $this->db->get_where('news', array('id' => $id))->row_array();
	}

	function get_latest_news(){
		$this->db->select('p.*')
			->from('news p')
			->order_by('p.id','desc')
			->limit(3);
		return $this->db->get()->result_array();
	}

	function delete($id)
	{
		if(empty($id)){
			return true;
		}
		$this->db->delete('news', array('id' => $id)); 
		return true;
	}
	
	function save($data='')
	{
		if(empty($data)){
			return false;
		}
		
		if (!empty($data['id'])) {
			$id = $data['id'];
			unset($data['id']);
			$this->db->set('update_date', 'now()', false);
			$this->db->update('news', $data, array('id' => $id));
			return $id;
		}else{
			$this->db->insert('news', $data);
			return $this->db->insert_id();
		}
	}
}